/**
 * 为String 添加substitute功能 实现模版替换功能
 * @author jeff
 * @param object
 * @param regexp
 * @returns
 */
String.prototype.substitute = function (object, regexp) {
    return this
        .replace(
        regexp || (/\\?\[([^\[\]]+)\]/g),
        function (match, name) {
            if (match.charAt(0) == '\\')
                return match.slice(1);
            return (object[name] != null) ? decodeURIComponent(object[name])
                : '';
        });
};

/**
 * 字符串格式化 在处理形如 10000.01 这样的有可能会出现问题 format('abcdefghijklmnopqrstuvwxyz', 6,
 * '-')); //result: ab-cdefgh-ijklmn-opqrst-uvwxyz
 * @author zhoubin
 */
function api_string_format(str, step, splitor) {
    str = str.toString();
    var len = str.length;

    if (len > step) {
        var l1 = len % step, l2 = parseInt(len / step), arr = [], first = str
            .substr(0, l1);
        if (first != '') {
            arr.push(first);
        }
        ;
        for (var i = 0; i < l2; i++) {
            arr.push(str.substr(l1 + i * step, step));
        }
        ;
        str = arr.join(splitor);
    }
    ;
    return str;
}

/**
 * 将传递过来的json对象转化为key=value&key1=value1的形式
 *
 * @param json_object
 * @author jeff
 */
function api_json_to_query(json_object) {
    var query_string = '';
    var first = true;
    if (typeof json_object == 'object') {
        jQuery.each(json_object, function (key, value) {
            if (first) {
                query_string += encodeURIComponent(key) + '='
                    + encodeURIComponent(value);
            }
            else {
                query_string += '&' + encodeURIComponent(key) + '='
                    + encodeURIComponent(value);
            }
            first = false;
        });
    }
    return query_string;
}

/**
 * Formats the number according to the 'format' string; adherses to the american
 * number standard where a comma is inserted after every 3 digits. note: there
 * should be only 1 contiguous number in the format, where a number consists of
 * digits, period, and commas any other characters can be wrapped around this
 * number, including '$', '%', or text examples (123456.789): '0' - (123456)
 * show only digits, no precision '0.00' - (123456.78) show only digits, 2
 * precision '0.0000' - (123456.7890) show only digits, 4 precision '0,000' -
 * (123,456) show comma and digits, no precision '0,000.00' - (123,456.78) show
 * comma and digits, 2 precision '0,0.00' - (123,456.78) shortcut method, show
 * comma and digits, 2 precision
 *
 * @method format
 * @param format
 *            {string} the way you would like to format this text
 * @return {string} the formatted number
 * @public 数字格式化方法 PS:使用之前,最好将操作数执行Number(num) 操作，将其转化为数字，
 *         否则有可能出现错误(字符串形式的数字不能调用的到这个format方法)
 *
 */
Number.prototype.format = function (format) {
    if ('string' != typeof (format)) {
        return '';
    } // sanity check

    var hasComma = (-1 != format.indexOf('.')), psplit = format.replace(
        /[^-\d\.]/g, '').split('.'), that = this, part_whole, part_decimal, temp;

    // compute precision
    if (1 < psplit.length) {// fix number precision
        that = that.toFixed(psplit[1].length);
    }
    else if (2 < psplit.length) {// error: too many periods
        throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
    }
    else {// remove precision
        that = that.toFixed(0);
    }

    formatted_num = that + '';
    // format has comma, then compute commas
    if (hasComma) {
        // remove precision for computation
        psplit = formatted_num.split('.');
        part_whole = psplit[0];
        part_decimal = psplit[1] || '';

        do
        {
            temp = part_whole;
            part_whole = part_whole.replace(/^(-?\d+)(\d{3})+/g, '$1,$2');
        } while (temp != part_whole);
        // add the precision back in
        formatted_num = part_whole
            + (part_decimal ? '.' + part_decimal : part_decimal);
    }

    // replace the number portion of the format with formatted_num
    return formatted_num;
};

/**
 * 实现类似php preg_quote的功能
 * 在正则表达式元字符的前面添加'\'进行转义
 *
 * @author jeff
 * powered by jeff 2011-06-07
 */
String.prototype.reg_quote = function (double_slash) {
    if (double_slash) {
        var reg_quote_str = this.replace(/\\/g, '\\\\\\');
        reg_quote_str = reg_quote_str.replace(/\./g, '\\\\.');
        reg_quote_str = reg_quote_str.replace(/\{/g, '\\\\{');
        reg_quote_str = reg_quote_str.replace(/\+/g, '\\\\+');
        reg_quote_str = reg_quote_str.replace(/\*/g, '\\\\*');
        reg_quote_str = reg_quote_str.replace(/\?/g, '\\\\?');
        reg_quote_str = reg_quote_str.replace(/\[/g, '\\\\[');
        reg_quote_str = reg_quote_str.replace(/\^/g, '\\\\^');
        reg_quote_str = reg_quote_str.replace(/\]/g, '\\\\]');
        reg_quote_str = reg_quote_str.replace(/\$/g, '\\\\$');
        reg_quote_str = reg_quote_str.replace(/\(/g, '\\\\(');
        reg_quote_str = reg_quote_str.replace(/\)/g, '\\\\)');
        reg_quote_str = reg_quote_str.replace(/\{/g, '\\\\{');
        reg_quote_str = reg_quote_str.replace(/\}/g, '\\\\}');
        reg_quote_str = reg_quote_str.replace(/\=/g, '\\\\=');
        reg_quote_str = reg_quote_str.replace(/\!/g, '\\\\!');
        reg_quote_str = reg_quote_str.replace(/\</g, '\\\\<');
        reg_quote_str = reg_quote_str.replace(/\>/g, '\\\\>');
        reg_quote_str = reg_quote_str.replace(/\|/g, '\\\\|');
        reg_quote_str = reg_quote_str.replace(/\:/g, '\\\\:');
        reg_quote_str = reg_quote_str.replace(/\-/g, '\\\\-');
    }
    else {
        var reg_quote_str = this.replace(/\\/g, '\\\\');
        reg_quote_str = reg_quote_str.replace(/\./g, '\\.');
        reg_quote_str = reg_quote_str.replace(/\{/g, '\\{');
        reg_quote_str = reg_quote_str.replace(/\+/g, '\\+');
        reg_quote_str = reg_quote_str.replace(/\*/g, '\\*');
        reg_quote_str = reg_quote_str.replace(/\?/g, '\\?');
        reg_quote_str = reg_quote_str.replace(/\[/g, '\\[');
        reg_quote_str = reg_quote_str.replace(/\^/g, '\\^');
        reg_quote_str = reg_quote_str.replace(/\]/g, '\\]');
        reg_quote_str = reg_quote_str.replace(/\$/g, '\\$');
        reg_quote_str = reg_quote_str.replace(/\(/g, '\\(');
        reg_quote_str = reg_quote_str.replace(/\)/g, '\\)');
        reg_quote_str = reg_quote_str.replace(/\{/g, '\\{');
        reg_quote_str = reg_quote_str.replace(/\}/g, '\\}');
        reg_quote_str = reg_quote_str.replace(/\=/g, '\\=');
        reg_quote_str = reg_quote_str.replace(/\!/g, '\\!');
        reg_quote_str = reg_quote_str.replace(/\</g, '\\<');
        reg_quote_str = reg_quote_str.replace(/\>/g, '\\>');
        reg_quote_str = reg_quote_str.replace(/\|/g, '\\|');
        reg_quote_str = reg_quote_str.replace(/\:/g, '\\:');
        reg_quote_str = reg_quote_str.replace(/\-/g, '\\-');
    }
    return reg_quote_str;
};

/**
 * @author jeff
 * powered by jeff 2011-06-08
 * js api 共用方法
 */
window.__common_prototype = function (ele, api_url) {
    if (typeof ele == 'string') {
        ele = jQuery('#' + ele);
    }
    else {
        ele = jQuery(ele);
    }
    var options = ele.attr('data');
    options = eval('(' + options + ')');
    defaults.url = api_url;
    var final_options = {};
    $.extend(final_options, defaults, options);
    api_ajax_request_get_info(ele, final_options);
};